package com.example.waterdispenserm2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.example.waterdispenserm2.util.SPUtils;
import com.example.waterdispenserm2.base.BasicsActivity;
import com.example.waterdispenserm2.bean.M2UIItem;
import com.example.waterdispenserm2.databinding.ActivityNoodleSetBinding;
import com.example.waterdispenserm2.dp.M2NoodleHelperUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

//模版设置
public class NoodleSetActivity extends BasicsActivity<ActivityNoodleSetBinding> {
    static M2UIItem Nm2UIItem;
    static int position;
    public static  void startActivity(M2UIItem m2UIItem,int type){
        Intent intent=new Intent(SPUtils.getContext(), NoodleSetActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Nm2UIItem=m2UIItem;
        position=type;
        SPUtils.getContext().startActivity(intent);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initView() {

        List<M2UIItem> list = M2NoodleHelperUtil.getInstance().getSQ();
        if (!TextUtils.isEmpty(list.get(position).getName())){
            binding.etName.setText(list.get(position).getName());

            if (!TextUtils.isEmpty(list.get(position).getTaste())){
                binding.etTaste.setText(list.get(position).getTaste());
            }
            if (!TextUtils.isEmpty(list.get(position).getAmountWater())){
                binding.etAmountOfWater.setText(list.get(position).getAmountWater());
            }
            if (!TextUtils.isEmpty(list.get(position).getDurationWater())){
                binding.etDuration.setText(list.get(position).getDurationWater());
            }
            if (!TextUtils.isEmpty(list.get(position).getHeatingDuration())){
                binding.etHeating.setText(list.get(position).getHeatingDuration());
            }
            if (!TextUtils.isEmpty(list.get(position).getWaterTemperature())){
                binding.etTemperature.setText(list.get(position).getWaterTemperature());
            }
            binding.ivDelete.setVisibility(View.VISIBLE);
        }
        binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                M2NoodleHelperUtil.getInstance().getUpData("","","","","","",(position+1)+"");
                System.out.println("---set---"+new Gson().toJson(M2NoodleHelperUtil.getInstance().getSQ()));
                finish();
            }
        });


        binding.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String etName=binding.etName.getText().toString();//名称
                if (!TextUtils.isEmpty(etName)){
                    String etTaste=binding.etTaste.getText().toString();//口感
                    String etDuration=binding.etDuration.getText().toString();//时长
                    String etHeating=binding.etHeating.getText().toString();//加热时长
                    String etTemperature=binding.etTemperature.getText().toString();//水温
                    String etAmountOfWater=binding.etAmountOfWater.getText().toString();//水温
                    if (!TextUtils.isEmpty(etAmountOfWater)){
                        if (Integer.parseInt(etAmountOfWater)>30&&Integer.parseInt(etAmountOfWater)< 3000){//判断设置的水量
                            if (Integer.parseInt(etTemperature)==0){//判断是不是要常温
                                M2NoodleHelperUtil.getInstance().getUpData(etName,etTaste,etDuration,etHeating,etTemperature,etAmountOfWater,(position+1)+"");
                                System.out.println("---set---"+new Gson().toJson(M2NoodleHelperUtil.getInstance().getSQ()));
                            }else {
                                if (Integer.parseInt(etTemperature)>=35&&Integer.parseInt(etTemperature)<= 98){//判断水温的范围
                                M2NoodleHelperUtil.getInstance().getUpData(etName,etTaste,etDuration,etHeating,etTemperature,etAmountOfWater,(position+1)+"");
                                System.out.println("---set---"+new Gson().toJson(M2NoodleHelperUtil.getInstance().getSQ()));
                            }else {
                                Toast.makeText(NoodleSetActivity.this, "温度设置范围为0或者(35-98°)", Toast.LENGTH_SHORT).show();
                            }
                            }
                        }else {
                            Toast.makeText(NoodleSetActivity.this, "水量设置(30-3000ml)", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                finish();
            }
        });
    }

    @Override
    protected ActivityNoodleSetBinding getViewBinding() {
        return ActivityNoodleSetBinding.inflate(LayoutInflater.from(this));
    }
}